﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using EnvDTE;
using EnvDTE80;

namespace LuomgAddin
{
    public abstract class DTECommand
    {
        public DTECommand()
        {
            this.Name = this.GetType().Name;
            this.Text = string.Empty;
            this.ToolTip = string.Empty;
        }

        public string Name { get; set; }
        public string Text { get; set; }
        public string ToolTip { get; set; }

        internal protected DTE2 DTE { get; internal set; }

        internal void Intialize()
        {
            this.IntializeCore();
        }

        protected virtual void IntializeCore() { }

        internal void Execute()
        {
            try
            {
                this.ExecuteCore();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Consts.Prefix + "异常！");
            }
        }

        internal vsCommandStatus QueryStatus()
        {
            return this.QueryStatusCore();
        }

        protected virtual vsCommandStatus QueryStatusCore()
        {
            return vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
        }

        protected abstract void ExecuteCore();
    }
    public class DTECommandMgr
    {
        public static readonly DTECommandMgr Instance = new DTECommandMgr();

        private DTECommandMgr() { }

        private List<DTECommand> _commands;

        public IList<DTECommand> GetAll()
        {
            if (this._commands == null)
            {
                this.LoadAll();
            }

            return this._commands;
        }

        public DTECommand GetCommand(string name)
        {
            return this.GetAll().FirstOrDefault(c => c.Name == name);
        }

        private void LoadAll()
        {
            //最下面的会生成在最上面
            this._commands = new List<DTECommand>
            {
                new CreateDependency(),
                new DropDependency(),
                new AddFileHeader(),
                new DeleteNullCommand(),
            };
        }
    }
}